package com.john.booter.domain.entity;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.validator.constraints.NotEmpty;

import com.fasterxml.jackson.annotation.JsonFormat;

/**
 * 系统用户
 * <pre>权限模型：用户-角色-资源</pre>
 * @author weny
 * @datetime 2015年9月18日 下午2:57:18
 */
@Entity
@Table(name = "t_user")
public class User implements Serializable {

	static final long serialVersionUID = -7139266244011946248L;
	
	@Id
	@GeneratedValue(generator = "uuid")
    @GenericGenerator(name = "uuid", strategy = "uuid")
    @Column(columnDefinition = "CHAR(32)")
	private String id;
	/**
	 * 用户名
	 */
	@NotEmpty
	@Column(nullable = false)
	private String username;
	/**
	 * 密码
	 */
	@NotEmpty
	@Column(unique=true, updatable=false, nullable = false)
	private String password;
	/**
	 * 真实姓名
	 */
	@Column
	private String trueName;
	/**
	 * 昵称
	 */
	@Column
	private String nickName;
	/**
	 * 状态
	 */
	@Column
	private String state;
	/**
	 * 手机号
	 */
	@Column
	private String cellphone;
	/**
	 * 创建时间
	 */
	@Column(nullable = true, updatable = false)
	@Temporal(TemporalType.TIMESTAMP)
	@JsonFormat(pattern = "yyyy-MM-dd")
	private Date createTime;
	/**
	 * 更新时间
	 */
	@Column(updatable = false)
	@Temporal(TemporalType.TIMESTAMP)
	@JsonFormat(pattern = "yyyy-MM-dd")
	private Date updateTime;
	/**
	 * 头像 
	 */
	@Column
	private String avatar;
	
	@ManyToOne
	@JoinColumn(name="role_code")
	private Role role;
	
	@PrePersist
	public void prePersist() {
		Date now = new Date();
		createTime = now;
	}
	
	@PreUpdate
	public void preUpdate() {
		updateTime = new Date();
	}

	// ==============
	// Getter and setter methods
	// ==============
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getTrueName() {
		return trueName;
	}

	public void setTrueName(String trueName) {
		this.trueName = trueName;
	}

	public String getNickName() {
		return nickName;
	}

	public void setNickName(String nickName) {
		this.nickName = nickName;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public String getCellphone() {
		return cellphone;
	}

	public void setCellphone(String cellphone) {
		this.cellphone = cellphone;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}
	
	public Date getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}

	public String getAvatar() {
		return avatar;
	}

	public void setAvatar(String avatar) {
		this.avatar = avatar;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

}
